function [xmlStruct, xml,status] = searchDASRegistry(varargin)
% SEARCHDASREGISTRY executes the specified query to interact with DAS Registry
%
% SEARCHDASREGISTRY queries the DAS Registry for finding DAS services.
% There are several possible ways to look for DAS services, including by
% label, organism, authority, capability, coordinate system type and URI.
% For a full documentation refer to http://www.dasregistry.org
%
% SYNTAX: [xmlStruct, xml,status] = searchDASRegistry(varargin)
%          xmlStruct - struct resulted from parsing DAS XML
%          xml - DAS XML
%          status - status returned by readDASURL
%
% EXAMPLES
% 
%     Search DAS Services by label
%     [xmlStruct, xml,status] = searchDASRegistry('label','biosapiens');
%     
%     Search DAS Services by organism
%     [xmlStruct, xml,status] = searchDASRegistry('organism','Homo sapiens');
%     
%     Search DAS Services by authority
%     [xmlStruct, xml,status] = searchDASRegistry('authority','Uniprot');
%     
%     Search DAS Services that accepts structure command (protein 3D data)
%     [xmlStruct, xml,status] = searchDASRegistry('capability','structure');
%     
%     Search the specific DAS Service specified by the URI
%     [xmlStruct, xml,status] = searchDASRegistry('URI','DS_409');
% 
% See also EXECUTEDASCOMMAND
%  
%    DASMiner: DAS library and browser for Matlab.
%    Diogo Veiga, March 2008.


p = inputParser;   % Create an instance of the class.

p.addParamValue('label', '', @ischar);
p.addParamValue('organism', '', @ischar); 
p.addParamValue('authority', '', @ischar);
p.addParamValue('capability', '', @(x) isValidCommand(x)); %check if it is a valid ID?
p.addParamValue('type', '', @ischar);
p.addParamValue('URI', '', @ischar);
p.addParamValue('timeout', 30000, @isnumeric); %time in miliseconds

p.KeepUnmatched = true;
p.FunctionName = 'SEARCHDASREGISTRY';
p.parse(varargin{:});



if (isempty(p.Results.label) && isempty(p.Results.organism) && isempty(p.Results.authority) && isempty(p.Results.capability) ...
        && isempty(p.Results.type) && isempty(p.Results.URI)) % Get all sources
    [xmlStruct, xml, status] = readDASURL('http://www.dasregistry.org/das1/sources/',p.Results.timeout);
    return;
end


if ( ( (~isempty(p.Results.label)) + (~isempty(p.Results.organism)) + (~isempty(p.Results.authority)) + (~isempty(p.Results.capability)) ...
        + (~isempty(p.Results.type)) + (~isempty(p.Results.URI)) ) > 1)
    error('The search is limited to 1 criterium at time');
end

if (~isempty(p.Results.label))
    dasURL = strcat('http://www.dasregistry.org/das1/sources?label=', char(java.net.URLEncoder.encode(p.Results.label,'UTF-8')));
    [xmlStruct, xml, status] = readDASURL(dasURL,p.Results.timeout);
    return;
end

if (~isempty(p.Results.organism))
    dasURL = strcat('http://www.dasregistry.org/das1/sources?organism=',char(java.net.URLEncoder.encode(p.Results.organism,'UTF-8')));
    [xmlStruct, xml, status] = readDASURL(dasURL,p.Results.timeout);
    return;
end

if (~isempty(p.Results.authority))
    dasURL = strcat('http://www.dasregistry.org/das1/sources?authority=',char(java.net.URLEncoder.encode(p.Results.authority,'UTF-8')));
    [xmlStruct, xml, status] = readDASURL(dasURL,p.Results.timeout);
    return;
end

if (~isempty(p.Results.capability))
    dasURL = strcat('http://www.dasregistry.org/das1/sources?capability=',char(java.net.URLEncoder.encode(p.Results.capability,'UTF-8')));
    [xmlStruct, xml, status] = readDASURL(dasURL,p.Results.timeout);
    return;
end

if (~isempty(p.Results.type))
    dasURL = strcat('http://www.dasregistry.org/das1/sources?type=',char(java.net.URLEncoder.encode(p.Results.type,'UTF-8')));
    [xmlStruct, xml, status] = readDASURL(dasURL,p.Results.timeout);
    return;
end

if (~isempty(p.Results.URI))
    dasURL = strcat('http://www.dasregistry.org/das1/sources/',char(java.net.URLEncoder.encode(p.Results.URI,'UTF-8')));
    [xmlStruct, xml, status] = readDASURL(dasURL,p.Results.timeout);
    return;
end


function [res] = isValidCommand(x)

res = any(strcmpi(x,{'dsn','entry_points', 'dna', 'sequence', 'types', 'features', 'stylesheet', 'structure', 'alignment','interaction'}));

function [res] = isValidURI(x)

match = regexp(x,'DS_\d*','match');
res = isempty(match);
